<?php

function smarty_function_compress($params, &$smarty)
{    
	static $sVersion;
	
	if (!$sVersion)
	{
		$sVersion = str_replace(array('.',' '), '', (App::getParam('info.version') . App::getParam('info.build')));
	}		
	
	if (!App::getParam('use_gzip') || defined('FORCE_NO_GZIP'))
	{	
		return App::getParam('url') . (substr($params['file'], 0, 5) != 'file/' ? PHPFOX_DEFAULT_DIR : '') . $params['file'] . "?v=" . $sVersion;
	}	

	if (isset($params['tinymce']))
	{
		return str_replace('tiny_mce.js', 'index.php', $params['file']);
	}
	
	if (isset($params['lytebox']))
	{
		return str_replace('lytebox.js', 'index.php', $params['file']) . "?v=" . $sVersion;
	}
	
	if (isset($params['theme'])) 
	{		
		$sThemeFile = str_replace('style.css', 'style.php', $params['file']);		

		if (file_exists(App::getParam('path') . $sThemeFile))
		{
			return $sThemeFile . "?v=" . $sVersion;			
		}
		
		if (is_writable(App::getParam('path') . str_replace('style.css', '', $params['file']))) 
		{
			$sData = '<?php' . "\n";
			$sData .= 'define("DIR", dirname(dirname(dirname(dirname(__FILE__)))) . "/");' . "\n";
			$sData .= 'require_once DIR . \'include/functions/Gzip.functions.php\';' . "\n";
			$sData .= 'doGzip(file_get_contents("./style.css"), "css");' . "\n";
			$sData .= '?>' . "\n";		
		
    		$hFile = fopen(App::getParam('path') . $sThemeFile, 'w');
    		fwrite($hFile, $sData);
    		fclose($hFile);
    		
    		return $sThemeFile . "?v=" . $sVersion;
		}
		
		return $params['file'] . "?v=" . $sVersion;
	}	
	
	$sFile = $params['file'];
	$sExt = (isset($params['css']) ? 'css' : 'js');
    $sNewFile = 'file/cache/' . md5($sFile . $sVersion) . '.' . $sExt;
    $sReturnFile = 'design/static/index.php?f=' . md5($sFile . $sVersion) . '&amp;t=' . $sExt . '&amp;v=' . $sVersion;
    
    if (file_exists(App::getParam('path') . $sNewFile)) 
    {
    	return $sReturnFile;
    }
    
	$sContent = file_get_contents($sFile);

    $hFile = fopen(App::getParam('path') . $sNewFile, 'w');
    fwrite($hFile, $sContent);
    fclose($hFile);   
      
    return $sReturnFile;
}

?>